package org.example.util;

import org.example.model.ApiResponse;

public class ApiResponseUtil<T> {

    private static final int SUCCESS_CODE = 200;
    private static final String SUCCESS_MESSAGE = "Success";
    private static final int FAIL_CODE = 500;
    private static final String FAIL_MESSAGE = "Fail";

    /**
     * 创建成功响应对象
     *
     * @param data 返回的数据
     * @param <T>  数据类型
     * @return ApiResponse 对象
     */
    public static <T> ApiResponse<T> success(T data) {
        return new ApiResponse<>(SUCCESS_CODE, SUCCESS_MESSAGE, data);
    }

    /**
     * 创建失败响应对象
     *
     * @param message 错误消息
     * @param <T>     数据类型
     * @return ApiResponse 对象
     */
    public static <T> ApiResponse<T> fail(String message) {
        return new ApiResponse<>(FAIL_CODE, null==message?FAIL_MESSAGE:message, null);
    }

    /**
     * 创建带有自定义错误码的失败响应对象
     *
     * @param code   错误码
     * @param message 错误消息
     * @param <T>    数据类型
     * @return ApiResponse 对象
     */
    public static <T> ApiResponse<T> fail(int code, String message) {
        return new ApiResponse<>(code, null==message?FAIL_MESSAGE:message, null);
    }
}
